/*****************************************************************
**          大连理工大学 凌BUG战队&大连民族大学 C.ONE战队
**                **生死有命，富贵在天**
**    没有什么难题是一个通宵解决不了的，如果有那就整两个！
**---------------------------------------------------------------
** 项目名称：   dlmu-hero-robot
** 日    期：   2024-03-12
** 作    者：   MasterWang & Changed by TuxMonkey
**---------------------------------------------------------------
** 文 件 名：   remote_control.h
** 文件说明：   遥控器及键鼠指令解析和机器人控制指令转换
*****************************************************************/
#ifndef REMOTE_CONTROL_H
#define REMOTE_CONTROL_H

/*---------------------INCLUDES----------------------*/
#include "main.h"

/*---------------------DEFINES-----------------------*/
//底盘XY移动的最大速度 mm/s
#define CHASSIS_X_MAXSPEED	 		 3500
#define CHASSIS_X_MINSPEED			-3000

#define CHASSIS_Y_MAXSPEED			 3000
#define CHASSIS_Y_MINSPEED			-3000

//云台俯仰最大角度值
#define GIMBAL_PITCH_MAXANGLE		 30.0f //30
#define GIMBAL_PITCH_MINANGLE	 	-20.0f //-50

//RC Switch Definition
#define RC_SW_UP                				1
#define RC_SW_MID               				3
#define RC_SW_DOWN              				2

//PC Key Definition
#define KEY_PRESSED_OFFSET_W            ((uint16_t)1 << 0)
#define KEY_PRESSED_OFFSET_S            ((uint16_t)1 << 1)
#define KEY_PRESSED_OFFSET_A            ((uint16_t)1 << 2)
#define KEY_PRESSED_OFFSET_D            ((uint16_t)1 << 3)
#define KEY_PRESSED_OFFSET_SHIFT        ((uint16_t)1 << 4)
#define KEY_PRESSED_OFFSET_CTRL         ((uint16_t)1 << 5)
#define KEY_PRESSED_OFFSET_Q            ((uint16_t)1 << 6)
#define KEY_PRESSED_OFFSET_E            ((uint16_t)1 << 7)
#define KEY_PRESSED_OFFSET_R            ((uint16_t)1 << 8)
#define KEY_PRESSED_OFFSET_F            ((uint16_t)1 << 9)
#define KEY_PRESSED_OFFSET_G            ((uint16_t)1 << 10)
#define KEY_PRESSED_OFFSET_Z            ((uint16_t)1 << 11)
#define KEY_PRESSED_OFFSET_X            ((uint16_t)1 << 12)
#define KEY_PRESSED_OFFSET_C            ((uint16_t)1 << 13)
#define KEY_PRESSED_OFFSET_V            ((uint16_t)1 << 14)
#define KEY_PRESSED_OFFSET_B            ((uint16_t)1 << 15)

//ROBOT Mode Definition
#define MOTOR_DISABLE								0
#define CHASSIS_FOLLOW_GIMBAL						1
#define CHASSIS_GYRO_ROTATION						2


/*---------------------STRUCTS-----------------------*/
//遥控器及键鼠指令数据结构
typedef struct 
{
	uint32_t recv_cnt;
	
  //joystick channel value
  int16_t ch1;
  int16_t ch2;
  int16_t ch3;
  int16_t ch4;
	
  //three_step switch value
  uint8_t sw_left; //sw1
  uint8_t sw_right; //sw2
	
  //mouse move_speed and button_press value
  struct {
    int16_t vx;
    int16_t vy;
    int16_t vz; //切换ui模式：高速模式、普通模式、调试模式、刷新调试数据(向前<->向后) 
    uint8_t press_l; //发弹（ctrl+press_l 单发）
    uint8_t press_r; //自瞄
  } mouse;
	
  //keyboard key value
  union {
    uint16_t key_code;
    struct
    {
      uint16_t W : 1; //前
      uint16_t S : 1; //后
      uint16_t A : 1; //左
      uint16_t D : 1; //右
      uint16_t SHIFT : 1; //加速
      uint16_t CTRL : 1; //组合控制键*
      uint16_t Q : 1; //开摩擦轮
      uint16_t E : 1; //关摩擦轮
      uint16_t R : 1; //开弹舱盖
      uint16_t F : 1; //复位状态(关弹舱盖、关小陀螺、关底盘旋转锁定、退出能量机关模式)
      uint16_t G : 1; //开小陀螺
      uint16_t Z : 1; //无视热量限制（ctrl+z 启动录像）
      uint16_t X : 1; //拨弹反转（ctrl+x 关闭录像）
      uint16_t C : 1; //能量机关中心R对准(需先ctrl+c 进入能量机关模式)
      uint16_t V : 1; //开底盘旋转锁定
      uint16_t B : 1; //刷新UI界面
    } key_bit;
  } keyboard;
	
  //left_top courner wheel value
  int16_t wheel; //向下拨弹正转，向上拨弹反转
	
	//switch change flag
	uint8_t sw_left_midtoup_flag:1;
	uint8_t sw_left_uptomid_flag:1;
	uint8_t sw_left_midtodown_flag:1;
	uint8_t sw_left_downtomid_flag:1;
	
	uint8_t sw_right_midtoup_flag:1;
	uint8_t sw_right_uptomid_flag:1;
	uint8_t sw_right_midtodown_flag:1;
	uint8_t sw_right_downtomid_flag:1;
	
} RC_ctrl_t;

//机器人控制指令数据结构
typedef struct
{
	uint8_t movement_mode;

	int16_t vx_set;
	int16_t vy_set;
	
	float yaw_angle_set;
	float yaw_angle_set_energy; //能量机关专用yaw角度
	float pitch_angle_set;
	
	uint8_t shoot_open_flag;
	uint8_t turnbullet_flag;
	uint8_t turnbullet_once_flag;
	uint8_t turnbullet_reverse_flag;
	uint8_t ignore_shoot_heat_limit_flag;
	uint8_t bulletroom_open_flag;
	uint8_t auto_aiming_flag;
	uint8_t energy_aiming_flag;
	
	uint8_t chassis_lock_flag;
	uint8_t chassis_accelerate_flag;

	uint8_t ui_show_mode;
	
} ROBOT_ctrl_t;

//自瞄控制指令数据结构
typedef struct
{
	uint32_t recv_cnt; //接收计数
	uint32_t recv_interval; //接收时间间隔
	
	uint8_t detect_mode; //自瞄模式：0000_0xxx 第2位:0-关录像/1-开录像 第1位:0-我红方/1-我蓝方 第0位:0-打机器人/1-打能量机关
	uint8_t detect_state; //识别状态：0x00-无效/0x01-有效
	
	uint16_t valid_count; //自瞄识别有效计数
	uint16_t invalid_count; //自瞄识别无效计数
	
	float yaw_delt_angle; //云台yaw轴相对角度
	float pitch_absolute_angle; //云台pitch轴绝对角度
	
	uint8_t shoot_control; //发射控制：0x00-不控制/0x01-单发/0x02-连发
	
} AutoAiming_ctrl_t;

/*---------------------DECLARES----------------------*/
extern RC_ctrl_t rc_ctrl;
extern uint8_t rc_update_flag;

extern ROBOT_ctrl_t robot_ctrl;

extern AutoAiming_ctrl_t autoaiming_ctrl;
extern uint8_t autoaiming_update_flag;

//
extern void dbus_to_rc(uint8_t *dbus_buf, RC_ctrl_t *rc_ctrl);
extern void rc_to_robot(RC_ctrl_t *rc_ctrl, ROBOT_ctrl_t *robot_ctrl);


#endif
